package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.CoUserAddDTO;
import com.xbongbong.paas.pojo.dto.CoUserDeleteDTO;
import com.xbongbong.paas.pojo.dto.CoUserListDTO;
import com.xbongbong.paas.pojo.vo.CoUserAddVO;
import com.xbongbong.paas.pojo.vo.CoUserDeleteVO;
import com.xbongbong.paas.pojo.vo.CoUserListVO;
import com.xbongbong.paas.service.CoUserService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/9/6 16:48
 * @desrcption: 协同人
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/coUser")
public class MobilePaasCoUserController {

    @Resource
    private CoUserService coUserService;
    /**
     * 协同人列表
     * @param coUserListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid CoUserListDTO coUserListDTO, BindingResult br) {
        XbbResponse<CoUserListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CoUserListVO synergeticListVO = coUserService.list(coUserListDTO);
                response = new XbbResponse<>(synergeticListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 增加协同人
     * @param coUserAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid CoUserAddDTO coUserAddDTO, BindingResult br) {
        XbbResponse<CoUserAddVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CoUserAddVO coUserAddVO = coUserService.add(coUserAddDTO);
                response = new XbbResponse<>(coUserAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除协同人
     * @param coUserDeleteDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid CoUserDeleteDTO coUserDeleteDTO, BindingResult br) {
        XbbResponse<CoUserDeleteVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CoUserDeleteVO coUserDeleteVO = coUserService.delete(coUserDeleteDTO);
                response = new XbbResponse<>(coUserDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
